package com.google.code.contactserv.func;

import com.google.code.contactserv.func.execution.ExecutorFactory;
import com.google.code.contactserv.func.model.ConsoleTest;
import com.google.code.contactserv.func.model.ContactservTestFactory;
import org.apache.commons.cli.*;

import java.io.PrintWriter;

/*
*       Copyright 2007 The semap project development team.
*
*       Licensed under the Apache License, Version 2.0 (the "License");
*       you may not use this file except in compliance with the License.
*       You may obtain a copy of the License at
*
*               http://www.apache.org/licenses/LICENSE-2.0
*
*       Unless required by applicable law or agreed to in writing, software
*       distributed under the License is distributed on an "AS IS" BASIS,
*       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*       See the License for the specific language governing permissions and
*       limitations under the License.
*
*       Created by ${user} at ${date} ${time}.
*
*       ${description}.
*
*       $Id$
*/

public class FuncTest {
    private static boolean isOK = true;

    public static void main(String args[]) {
        CommandLineParser parser = new PosixParser();
        Options opt = createOptions();
        CommandLine line = null;
        PrintWriter writer = new PrintWriter(System.out);
        try {
            line = parser.parse(opt, args);
        }
        catch (ParseException exp) {
            System.out.println("Unexpected exception:" + exp.getMessage());
        }

        if(line.hasOption('h')){
            showHelp(opt, writer);
            System.exit(0);
        }

        TestRunner runner = new TestRunner(writer);
        try {
            runner.setExecutor(ExecutorFactory.getInstance(line));
        } catch(IllegalArgumentException e){
            writer.println("Error preparing tests:\n" + e.toString());
            showHelp(opt, writer);
        }
        for(ConsoleTest test : new ContactservTestFactory().getTestScenario()){
            if(!runner.executeTest(test))
              reportFault(test.getName(), runner.getExecutionDescription(), writer);
        }
        if(isOK)
          writer.println("Tests PASSED!");
        writer.flush();
        writer.close();
    }

    private static void reportFault(String name, String executionDescription, PrintWriter writer) {
        writer.println("Test FAILD!");
        writer.println("Test: " + name);
        writer.println("Description: " + executionDescription);
        writer.println("\n");
        writer.flush();
        isOK = false;
    }

    private static void showHelp(Options opt, PrintWriter writer){
        HelpFormatter hf = new HelpFormatter();
        hf.printHelp(writer, 70, "FuncTest", "Contact service functional test usage", opt, 7, 7, "", true);
        writer.flush();
    }

    private static Options createOptions() {
        Options opt = new Options();
        opt.addOption("c", "class", true, "Specified class will be tested");
        opt.addOption("f", "file",  true, "Specify command file to be tested");
        opt.addOption("p", "path",  true, "Working folder. Only used if tested file is specified.");
        opt.addOption("h", "help",  false, "Display this help");
        return opt;
    }

}
